<?php
/**
 * NotORM - simple reading data from the database
 * @link http://www.notorm.com/
 * @author Jakub Vrana, http://www.vrana.cz/
 * @copyright 2010 Jakub Vrana
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
 */
namespace DB\Models\NotORM;

/**
 * Database representation
 * @property mixed $debug = false Enable debugging queries, true for error_log($query), callback($query, $parameters) otherwise
 * @property bool $freeze = false Disable persistence
 * @property string $rowClass = 'NotORM_Row' Class used for created objects
 * @property bool $jsonAsArray = false Use array instead of object in Result JSON serialization
 * @property string $transaction Assign 'BEGIN', 'COMMIT' or 'ROLLBACK' to start or stop transaction
 */
class NotORM extends NotORM_Abstract
{
    /**
     * Create database representation
     * @param PDO
     * @param NotORM_Structure or null for new NotORM_Structure_Convention
     * @param NotORM_Cache or null for no cache
     */
    public function __construct(\PDO $connection, NotORM_Structure $structure = null, NotORM_Cache $cache = null)
    {
        $this->connection = $connection;
        $this->driver = $connection->getAttribute(\PDO::ATTR_DRIVER_NAME);
        if (!isset($structure)) {
            $structure = new NotORM_Structure_Convention;
        }
        $this->structure = $structure;
        $this->cache = $cache;
    }

    /**
     * Get table data to use as $db->table[1]
     * @param string
     * @param mixed $table
     * @return NotORM_Result
     */
    public function __get($table)
    {
        return new NotORM_Result($this->structure->getReferencingTable($table, ''), $this, true);
    }

    /**
     * Set write-only properties
     * @return null
     * @param mixed $name
     * @param mixed $value
     */
    public function __set($name, $value)
    {
        if ($name == 'debug' || $name == 'debugTimer' || $name == 'freeze' || $name == 'rowClass' || $name == 'jsonAsArray') {
            $this->$name = $value;
        }
        if ($name == 'transaction') {
            switch (strtoupper($value)) {
                case 'BEGIN': return $this->connection->beginTransaction();
                case 'COMMIT': return $this->connection->commit();
                case 'ROLLBACK': return $this->connection->rollback();
            }
        }
    }

    /**
     * Get table data
     * @param string
     * @param array (["condition"[, array("value")]]) passed to NotORM_Result::where()
     * @param mixed $table
     * @return NotORM_Result
     */
    public function __call($table, array $where)
    {
        $return = new NotORM_Result($this->structure->getReferencingTable($table, ''), $this);
        if ($where) {
            call_user_func_array([$return, 'where'], $where);
        }
        return $return;
    }
}
